//
//  ZXZHomeViewController.m
//  zuxianzhi
//
//  Created by tyu on 27.5.24.
//

#import "ZXZHomeViewController.h"
#import "ZXZHomeMoreViewController.h"
#import "ZXZGoodsView.h"
#import "ZXZHomeGoodsViewController.h"
#import "ZXZLoginViewController.h"

@interface ZXZHomeViewController ()<UIScrollViewDelegate>

@property(nonatomic,weak) UIImageView *topImageV;
@property(nonatomic,weak) UILabel *titleLeftLab;
@property(nonatomic,weak) UIButton *phoneBtn;
@property(nonatomic,weak) UIScrollView *ScrollView;
@property(nonatomic,weak) ZXZGoodsView *goodsView1;
@property(nonatomic,weak) ZXZGoodsView *goodsView2;
@property(nonatomic,weak) ZXZGoodsView *goodsView3;

@end

@implementation ZXZHomeViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    if (self.navigationController.childViewControllers.count == 1) {
        self.tabBarController.tabBar.hidden = NO;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    [self updateNavigationBarShow];
}

- (void)updateNavigationBarShow {
    NSLog(@"%f",self.ScrollView.contentOffset.y);
    self.navView.hidden = self.ScrollView.contentOffset.y <= 0;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    self.view.backgroundColor  = [UIColor greenColor];
    
}

- (void)bac_addUI{
    [super bac_addUI];
    
    [self setupView];
    self.bacTitleLabel.text = @"万款游戏";
    self.bacbBackBtn.hidden = YES;
    self.navView.backgroundColor = UIColor.clearColor;
    [self.view bringSubviewToFront:self.navView];
}


- (void)setupView {

    UIImageView *topImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_topImageV"]];
    [self.view addSubview:topImageV];
    self.topImageV = topImageV;
    topImageV.userInteractionEnabled = YES;
    self.topImageV.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth * 244 / 750);
    
    UILabel *titleLeftLab = [[UILabel alloc]init];
    [self.topImageV addSubview:titleLeftLab];
    self.titleLeftLab =titleLeftLab;
    titleLeftLab.text = [NSString stringWithFormat:@"只为游戏而生"];
    titleLeftLab.textColor = [UIColor blackColor];
    titleLeftLab.font = [UIFont systemFontOfSize:20 weight:(UIFontWeightSemibold)];
    titleLeftLab.frame = CGRectMake(20, CGRectGetMaxY(self.topImageV.frame)-40-10, 200, 40);
    
    UIButton *phoneBtn = [[UIButton alloc] init];
    [self.topImageV addSubview:phoneBtn];
    self.phoneBtn = phoneBtn;
    [phoneBtn setImage:[UIImage imageNamed:@"home_ phone"] forState:UIControlStateNormal];
    phoneBtn.frame = CGRectMake(ScreenWidth-30-15, CGRectGetMaxY(self.topImageV.frame)-20-12, 20, 20);
    [phoneBtn addTarget:self action:@selector(phoneBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIScrollView *ScrollV = [[UIScrollView alloc]init];
    [self.view addSubview:ScrollV];
    self.ScrollView =ScrollV;
    ScrollV.delegate = self;
    
    ScrollV.contentSize = CGSizeMake(ScreenWidth, ScreenHeight * 1.4);
    ScrollV.contentInset = UIEdgeInsetsMake(0, 0, -10, 0);
    ScrollV.showsVerticalScrollIndicator = NO;
    ScrollV.frame = CGRectMake(0, CGRectGetHeight(self.topImageV.frame), ScreenWidth, ScreenHeight-ShowDiff - CGRectGetHeight(self.topImageV.frame));
    
    UIImageView *topViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_top_view_ImageV"]];
    [self.ScrollView addSubview:topViewImage];
    topViewImage.frame = CGRectMake(10, 0, ScreenWidth - 20, (ScreenWidth-20) * 668 / 672);
    
    topViewImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topViewImageClick)];
    [topViewImage addGestureRecognizer:Tap];
    
    UIImageView *home_more_left_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_more_left_image"]];
    [self.ScrollView addSubview:home_more_left_image];
    home_more_left_image.frame = CGRectMake(0, CGRectGetMaxY(topViewImage.frame)+10, ScreenWidth, (ScreenWidth)/7.31);
    
    home_more_left_image.userInteractionEnabled = YES;
    UITapGestureRecognizer *Tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(home_more_left_imageClick)];
    [home_more_left_image addGestureRecognizer:Tap2];

    CGFloat width = (ScreenWidth-30)/2;
    
    for (int i = 0; i<3; i++) {
        ZXZGoodsView *goods = [[ZXZGoodsView alloc] init];
        [self.ScrollView addSubview:goods];
        if (i==0) {
            goods.ImgV.image = [UIImage imageNamed:@"goods1"];
            goods.TitleLab.text = @"Insta360 x4";
            goods.SubLab.text = @"专业摄像";
            goods.imageWidth = width;
            self.goodsView1 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [self pushIndex:1];
            };
        }else if (i==1) {
            goods.ImgV.image = [UIImage imageNamed:@"GoPro_02"];
            goods.TitleLab.text = @"GoPro";
            goods.SubLab.text = @"专业防抖";
            goods.imageWidth = width;
            self.goodsView2 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [self pushIndex:2];
            };
        }else {
            goods.ImgV.image = [UIImage imageNamed:@"DJI_03"];
            goods.TitleLab.text = @"DJI";
            goods.SubLab.text = @"全景摄像";
            goods.imageWidth = width;
            self.goodsView3 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [self pushIndex:3];
            };
        }
    }
    
    CGFloat height = 250;
    self.goodsView1.frame = CGRectMake(10, CGRectGetMaxY(home_more_left_image.frame), width, height);
    self.goodsView2.frame = CGRectMake(CGRectGetMaxX(self.goodsView1.frame)+10, CGRectGetMinY(self.goodsView1.frame), width, height);
    self.goodsView3.frame = CGRectMake(10, CGRectGetMaxY(self.goodsView1.frame)+10, width, height);
}


- (void)pushIndex:(int)index {
    [self checkIsLogin:^{
        ZXZHomeGoodsViewController *vc = [ZXZHomeGoodsViewController new];
        vc.index = index;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
}

- (void)topViewImageClick {
    // 产品1
    [self checkIsLogin:^{
        ZXZHomeGoodsViewController *vc = [ZXZHomeGoodsViewController new];
        vc.index = 1;
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
}

- (void)home_more_left_imageClick {
    // 更多
    [self checkIsLogin:^{
        ZXZHomeMoreViewController *vc = [ZXZHomeMoreViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
}

// 电话
- (void)phoneBtnClick {
    [self showKefuAlertView];
}

- (void)showKefuAlertView {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:defServerPhone preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:NULL];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)checkIsLogin:(void(^)(void))complete {
    if ([[userDef valueForKey:islogin] intValue] != 1) {
        [self.navigationController pushViewController:[ZXZLoginViewController new] animated:YES];
        return;
    }else{
        if (complete){
            complete();
        }
    }
}

@end
